home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / lib / gcc-lib / djgpp / 2.952 / units / gpcutil.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-08  |  12.6 KB  |  414 lines

  1. {
  2. Some utility routines for compatibility to some units available for
  3. BP, like some `Turbo Power' units.
  4.  
  5. @@NOTE - SOME OF THE ROUTINES IN THIS UNIT MAY NOT WORK CORRECTLY.
  6. TEST CAREFULLY AND USE WITH CARE!
  7.  
  8. Copyright (C) 1998-2001 Free Software Foundation, Inc.
  9.  
  10. Authors: Prof. Abimbola A. Olowofoyeku <African_Chief@bigfoot.com>
  11.          Frank Heckenbach <frank@pascal.gnu.de>
  12.  
  13. This file is part of GNU Pascal.
  14.  
  15. GNU Pascal is free software; you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation; either version 2, or (at your option)
  18. any later version.
  19.  
  20. GNU Pascal is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with GNU Pascal; see the file COPYING. If not, write to the
  27. Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  28. 02111-1307, USA.
  29.  
  30. As a special exception, if you link this file with files compiled
  31. with a GNU compiler to produce an executable, this does not cause
  32. the resulting executable to be covered by the GNU General Public
  33. License. This exception does not however invalidate any other
  34. reasons why the executable file might be covered by the GNU General
  35. Public License.
  36. }
  37.  
  38. {$gnu-pascal,B-,I-}
  39. {$if __GPC_RELEASE__ < 20000412}
  40. {$error This unit requires GPC release 20000412 or newer.}
  41. {$endif}
  42.  
  43. unit GPCUtil;
  44.  
  45. interface
  46.  
  47. uses GPC;
  48.  
  49. { Replace all occurences of OldC with NewC in s and return the
  50.   result }
  51. function  ReplaceChar (const s : String; OldC, NewC : Char) : TString;
  52.  
  53. { Return the current working directory }
  54. function  ThisDirectory : TString; asmname '_p_get_current_directory';
  55.  
  56. { Does a directory exist? }
  57. function  IsDirectory (const aFileName : String) : Boolean; asmname '_p_directory_exists';
  58.  
  59. { Break a string into 2 parts, using Ch as a marker }
  60. function  BreakStr (const Src : String; var Dest1, Dest2 : String; Ch : Char) : Boolean;
  61.  
  62. { Convert a CString to an Integer }
  63. function  PChar2Int (s : CString) : Integer;
  64.  
  65. { Convert a CString to a LongInt }
  66. function  PChar2Long (s : CString) : LongInt;
  67.  
  68. { Convert a CString to a Double }
  69. function  PChar2Double (s : CString) : Double;
  70.  
  71. { Search for s as an executable in the path and return its location
  72.   (full pathname) }
  73. function  PathLocate (const s : String) : TString;
  74.  
  75. { Copy file Src to Dest and return the number of bytes written }
  76. function  CopyFile (const Src, Dest : String; BufSize : Integer) : LongInt;
  77.  
  78. { Copy file Src to Dest and return the number of bytes written;
  79.   report the number of bytes written versus total size of the source
  80.   file }
  81. function  CopyFileEx (const Src, Dest : String; BufSize : Integer;
  82.   function Report (Reached, Total : LongInt) : LongInt) : LongInt;
  83.  
  84. { Turbo Power compatibility }
  85.  
  86. { Execute the program prog. Dummy1 and Dummy2 are for compatibility
  87.   only; they are ignored. }
  88. function  ExecDos (const Prog : String; Dummy1 : Boolean; Dummy2 : Pointer) : Integer;
  89.  
  90. { Return whether Src exists in the path as an executable -- if so
  91.   return its full location in Dest }
  92. function  ExistOnPath (const Src : String; var Dest : String) : Boolean;
  93.  
  94. { Does file name s exist? }
  95. function  ExistFile (const aFileName : String) : Boolean; asmname '_p_file_exists';
  96.  
  97. { Return just the directory path of Path. Returns DirSelf +
  98.   DirSeparator if Path contains no directory. }
  99. function  JustPathName (const Path : String) : TString; asmname '_p_dir_from_path';
  100.  
  101. { Return just the file name part without extension of Path. Empty if
  102.   Path contains no file name. }
  103. function  JustFileName (const Path : String) : TString; asmname '_p_name_from_path';
  104.  
  105. { Return just the extension of Path. Empty if Path contains no
  106.   extension. }
  107. function  JustExtension (const Path : String) : TString; asmname '_p_ext_from_path';
  108.  
  109. { Change the extension of s to Ext (do not include the dot!) }
  110. function  ForceExtension (const s, Ext : String) : TString;
  111.  
  112. { Return the full pathname of Path }
  113. function  FullPathName (const Path : String) : TString; asmname '_p_fexpand';
  114.  
  115. { Add a DirSeparator to the end of s if there is not already one }
  116. function  AddBackSlash (const s : String) : TString; asmname '_p_forceadddirseparator';
  117.  
  118. { Convert Integer to PChar; uses CStringNew to allocate memory for
  119.   the result, so you must call StrDispose to free the memory later }
  120. function  Int2PChar (i : Integer) : PChar;
  121.  
  122. { Convert Integer to string }
  123. function  Int2Str (i : Integer) : TString;
  124.  
  125. { Convert string to Integer }
  126. function  Str2Int (const s : String; var i : Integer) : Boolean;
  127.  
  128. { Convert string to LongInt }
  129. function  Str2Long (const s : String; var i : LongInt) : Boolean;
  130.  
  131. { Convert string to Double }
  132. function  Str2Real (const s : String; var i : Double) : Boolean;
  133.  
  134. { Return a string stripped of leading spaces }
  135. function  TrimLead (const s : String) : TString; asmname '_p_trimleft_str';
  136.  
  137. { Return a string stripped of trailing spaces }
  138. function  TrimTrail (const s : String) : TString; asmname '_p_trimright_str';
  139.  
  140. { Return a string stripped of leading and trailing spaces }
  141. function  Trim (const s : String) : TString; asmname '_p_trimboth_str';
  142.  
  143. { Return a string right-padded to length Len with ch }
  144. function  PadCh (const s : String; ch : Char; Len : Integer) : TString;
  145.  
  146. { Return a string right-padded to length Len with spaces }
  147. function  Pad (const s : String; Len : Integer) : TString;
  148.  
  149. { Return a string left-padded to length Len with ch }
  150. function  LeftPadCh (const s : String; ch : Char; Len : Byte) : TString;
  151.  
  152. { Return a string left-padded to length Len with blanks }
  153. function  LeftPad (const s : String; Len : Integer) : TString;
  154.  
  155. { Convert a string to lowercase }
  156. function  StLoCase (const s : String) : TString; asmname '_p_locase_str';
  157.  
  158. { Convert a string to uppercase }
  159. function  StUpCase (const s : String) : TString; asmname '_p_upcase_str';
  160.  
  161. { Uniform access to big memory blocks for GPC and BP. Of course, for
  162.   programs that are meant only for GPC, you can use the usual New/Dispose
  163.   routines. But for programs that should compile with GPC and BP, you can
  164.   use the following routines for GPC. In the GPC unit for BP (gpc-bp.pas),
  165.   you can find emulations for BP that try to provide access to as much
  166.   memory as possible, despite the limitations of BP. The backdraw is that
  167.   this memory cannot be used freely, but only with the following moving
  168.   routines. }
  169.  
  170. type
  171.   PBigMem = ^TBigMem;
  172.   TBigMem (MaxNumber : Integer) = record
  173.     { Public fields }
  174.     Number, BlockSize : SizeType;
  175.     Mappable : Boolean;
  176.     { Private fields }
  177.     Pointers : array [1 .. MaxNumber] of ^Byte
  178.   end;
  179.  
  180. { Note: the number of blocks actually allocated may be smaller than
  181.   WantedNumber. Check the Number field of the result. }
  182. function  AllocateBigMem (WantedNumber, aBlockSize : SizeType; WantMappable : Boolean) : PBigMem;
  183. procedure DisposeBigMem (p : PBigMem);
  184. procedure MoveToBigMem (var Source; p : PBigMem; BlockNumber : SizeType);
  185. procedure MoveFromBigMem (p : PBigMem; BlockNumber : SizeType; var Dest);
  186. { Maps a big memory block into normal addressable memory and returns
  187.   its address. The memory must have been allocated with
  188.   WantMappable = True. The mapping is only valid until the next
  189.   MapBigMem call. }
  190. function  MapBigMem (p : PBigMem; BlockNumber : SizeType) : Pointer;
  191.  
  192. implementation
  193.  
  194. function PathLocate (const s : String) : TString;
  195. begin
  196.   PathLocate := FSearchExecutable (s, GetEnv (PathEnvVar))
  197. end;
  198.  
  199. function ExistOnPath (const Src : String; var Dest : String) = Existing : Boolean;
  200. begin
  201.   Dest := PathLocate (Src);
  202.   Existing := Dest <> '';
  203.   if Existing then Dest := FExpand (Dest)
  204. end;
  205.  
  206. function ForceExtension (const s, Ext : String) = Res : TString;
  207. var i : Integer;
  208. begin
  209.   i := LastPos (ExtSeparator, s);
  210.   if (i = 0) or (CharPosFrom (DirSeparators, s, i) <> 0)
  211.     then Res := s
  212.     else Res := Copy (s, 1, i - 1);
  213.   if (Ext <> '') and (Ext [1] <> ExtSeparator) then Res := Res + ExtSeparator;
  214.   Res := Res + Ext
  215. end;
  216.  
  217. function ExecDos (const Prog : String; Dummy1 : Boolean; Dummy2 : Pointer) : Integer;
  218. var
  219.   Dummy_1 : Boolean;
  220.   Dummy_2 : Pointer;
  221. begin
  222.   Dummy_1 := Dummy1;
  223.   Dummy_2 := Dummy2;
  224.   ExecDos := Execute (Prog)
  225. end;
  226.  
  227. function PadCh (const s : String; ch : Char; Len : Integer) = Padded : TString;
  228. begin
  229.   Padded := s;
  230.   if Length (Padded) < Len then Padded := Padded + StringOfChar (ch, Len - Length (Padded))
  231. end;
  232.  
  233. function Pad (const s : String; Len : Integer) : TString;
  234. begin
  235.   Pad := PadCh (s, ' ', Len)
  236. end;
  237.  
  238. function LeftPadCh (const s : String; ch : Char; Len : Byte) = Padded : TString;
  239. begin
  240.   Padded := s;
  241.   if Length (Padded) < Len then Padded := StringOfChar (ch, Len - Length (Padded)) + Padded
  242. end;
  243.  
  244. function LeftPad (const s : String; Len : Integer) : TString;
  245. begin
  246.   LeftPad := LeftPadCh (s, ' ', Len)
  247. end;
  248.  
  249. function Int2PChar (i : Integer) : PChar;
  250. var s : TString;
  251. begin
  252.   Str (i, s);
  253.   Int2PChar := CStringNew (s)
  254. end;
  255.  
  256. function Int2Str (i : Integer) = s : TString;
  257. begin
  258.   Str (i, s)
  259. end(*@@*)(*$local W-*);(*$endlocal*)
  260.  
  261. function Str2Int (const s : String; var i : Integer) : Boolean;
  262. var e : Integer;
  263. begin
  264.   Val (s, i, e);
  265.   Str2Int := e = 0
  266. end;
  267.  
  268. function Str2Long (const s : String; var i : LongInt) : Boolean;
  269. var e : Integer;
  270. begin
  271.   Val (s, i, e);
  272.   Str2Long := e = 0
  273. end;
  274.  
  275. function Str2Real (const s : String; var i : Double) : Boolean;
  276. var e : Integer;
  277. begin
  278.   Val (s, i, e);
  279.   Str2Real := e = 0
  280. end;
  281.  
  282. function CopyFile (const Src, Dest : String; BufSize : Integer) : LongInt;
  283. begin
  284.   CopyFile := CopyFileEx (Src, Dest, BufSize, nil)
  285. end;
  286.  
  287. function CopyFileEx (const Src, Dest : String; BufSize : Integer;
  288.   function Report (Reached, Total : LongInt) : LongInt) = BytesCopied : LongInt;
  289. var
  290.   Size : LongInt;
  291.   Count : Integer;
  292.   SrcFile, DestFile : File;
  293.   Buf : ^Byte;
  294.   B : BindingType;
  295. begin
  296.   Reset (SrcFile, Src, 1);
  297.   if IOResult <> 0 then
  298.     begin
  299.       BytesCopied := - 2;
  300.       Exit
  301.     end;
  302.   Rewrite (DestFile, Dest, 1);
  303.   if IOResult <> 0 then
  304.     begin
  305.       Close (SrcFile);
  306.       BytesCopied := - 3;
  307.       Exit
  308.     end;
  309.   B := Binding (SrcFile);
  310.   Size := FileSize (SrcFile);
  311.   GetMem (Buf, BufSize);
  312.   BytesCopied := 0;
  313.   repeat
  314.     BlockRead (SrcFile, Buf^, BufSize, Count);
  315.     Inc (BytesCopied, Count);
  316.     if IOResult <> 0 then
  317.       BytesCopied := - 100 { Read error }
  318.     else if Count > 0 then
  319.       begin
  320.         BlockWrite (DestFile, Buf^, Count);
  321.         if IOResult <> 0 then
  322.           BytesCopied := - 200 { Write error }
  323.         else if Assigned (Report) and_then (Report (BytesCopied, Size) < 0) then
  324.           BytesCopied := - 300 { User Abort }
  325.       end
  326.   until (BytesCopied < 0) or (Count = 0);
  327.   FreeMem (Buf);
  328.   Close (SrcFile);
  329.   if BytesCopied >= 0 then
  330.     begin
  331.       SetFileTime ((*@@AnyFile*)AnyFile( DestFile), GetUnixTime (null), B.ModificationTime);
  332.       ChMod ((*@@AnyFile*)AnyFile( DestFile), B.Mode)
  333.     end;
  334.   Close (DestFile)
  335. end;
  336.  
  337. function BreakStr (const Src : String; var Dest1, Dest2 : String; Ch : Char) : Boolean;
  338. var i : Integer;
  339. begin
  340.   i := Pos (Ch, Src);
  341.   BreakStr := i <> 0;
  342.   if i = 0 then i := Length (Src) + 1;
  343.   Dest1 := Trim (Copy (Src, 1, i - 1));
  344.   Dest2 := Trim (Copy (Src, i + 1))
  345. end;
  346.  
  347. {$local X+}
  348. function PChar2Int (s : CString) = i : Integer;
  349. begin
  350.   ReadStr (s, i)
  351. end;
  352.  
  353. function PChar2Long (s : CString) = i : LongInt;
  354. begin
  355.   ReadStr (s, i)
  356. end;
  357.  
  358. function PChar2Double (s : CString) = x : Double;
  359. begin
  360.   ReadStr (s, x)
  361. end;
  362. {$endlocal}
  363.  
  364. function ReplaceChar (const s : String; OldC, NewC : Char) = Res : TString;
  365. var i : Integer;
  366. begin
  367.   Res := s;
  368.   if OldC <> NewC then
  369.     for i := 1 to Length (Res) do
  370.       if Res [i] = OldC then Res [i] := NewC
  371. end;
  372.  
  373. function AllocateBigMem (WantedNumber, aBlockSize : SizeType; WantMappable : Boolean) = p : PBigMem;
  374. begin
  375.   New (p, WantedNumber);
  376.   with p^ do
  377.     begin
  378.       Mappable := WantMappable;
  379.       BlockSize := aBlockSize;
  380.       Number := 0;
  381.       while Number < WantedNumber do
  382.         begin
  383.           Pointers [Number + 1] := CGetMem (BlockSize);
  384.           if Pointers [Number + 1] = nil then Break;
  385.           Inc (Number)
  386.         end
  387.     end
  388. end;
  389.  
  390. procedure DisposeBigMem (p : PBigMem);
  391. var i : Integer;
  392. begin
  393.   for i := 1 to p^.Number do CFreeMem (p^.Pointers [i]);
  394.   Dispose (p)
  395. end;
  396.  
  397. procedure MoveToBigMem (var Source; p : PBigMem; BlockNumber : SizeType);
  398. begin
  399.   Move (Source, p^.Pointers [BlockNumber]^, p^.BlockSize)
  400. end;
  401.  
  402. procedure MoveFromBigMem (p : PBigMem; BlockNumber : SizeType; var Dest);
  403. begin
  404.   Move (p^.Pointers [BlockNumber]^, Dest, p^.BlockSize)
  405. end;
  406.  
  407. function MapBigMem (p : PBigMem; BlockNumber : SizeType) : Pointer;
  408. begin
  409.   if not p^.Mappable then RuntimeError (857); { attempt to map unmappable memory }
  410.   MapBigMem := p^.Pointers [BlockNumber]
  411. end;
  412.  
  413. end.
  414.